package com.example.ywj.toutiao;

import android.content.Intent;
import android.graphics.Typeface;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ywj.toutiao.bean.User;
import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.example.ywj.toutiao.utils.SpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "PasswordActivity";
    private String baseUrl = Constant.BASEURL;
    @BindView(R.id.password_back)
    public TextView password_back;
    @BindView(R.id.password_next)
    public TextView password_next;
    @BindView(R.id.password)
    public EditText password;
    public String username;
    public Boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.trgray));
        }
        overridePendingTransition(0, 0);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        //Log.i(TAG, "onCreate: "+username);
        initView();
    }
    public void initView(){
        Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        password_back.setTypeface(font);
        password_next.setTypeface(font);
        password_back.setText(getResources().getString(R.string.cha));
        password_next.setText(getResources().getString(R.string.next));
        password_back.setOnClickListener(this);
        password_next.setOnClickListener(this);
        password.addTextChangedListener(new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                login(String.valueOf(s),username);
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    public void login(String password,String username){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.login(username,password);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200 && obj.has("data")){
                    password_next.setBackground(getDrawable(R.drawable.border_textview2));
                    password_next.setTextColor(getColor(R.color.white));
                    JsonArray objArr = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    List<User> newList = gson.fromJson(objArr,new TypeToken<ArrayList<User>>(){}.getType());
                    String username = newList.get(0).getUsername();
                    SpUtils.putString(PasswordActivity.this,"username",username);
                    //Log.i(TAG, "onResponse: "+username);
                    flag = true;
                }else{
                    flag = false;
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.password_back:
                finish();
                break;
            case R.id.password_next:
                if(flag){
                    flag = false;
                    SpUtils.putBoolean(PasswordActivity.this,"isreturn",false);
                    Intent intent = new Intent(PasswordActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }else{
                    Toasty.error(PasswordActivity.this, "密码错误", Toast.LENGTH_SHORT, true).show();
                }
                break;
        }
    }

    @Override
    protected void onPause() {
        overridePendingTransition(0, 0);
        super.onPause();
    }
}
